package com.sdgakj.utils.pinyin;

import com.sdgakj.dto.CityDTO;
import com.sdgakj.entity.ShtCity;
import com.sdgakj.entity.ShtProvince;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PinYinSort
{
    
    public static void main(String[] args)
    {
        
        ShtProvince province = new ShtProvince();
        province.setName("123");
        ShtProvince province1 = new ShtProvince();
        province1.setName("重庆");
        ShtProvince province2 = new ShtProvince();
        province2.setName("北京");
        ShtProvince province3 = new ShtProvince();
        province3.setName("日本");
        List<ShtProvince> provinces = new ArrayList<ShtProvince>();
        provinces.add(province);
        provinces.add(province1);
        provinces.add(province2);
        provinces.add(province3);
        ProvinceListSort(provinces);
        for(ShtProvince shtProvince : provinces)
        {
            System.out.println(shtProvince.getName());
        }
        
    }
    
    public static void ProvinceListSort(List<ShtProvince> arr)
    {
        Collections.sort(arr, new ComparatorProvince());
    }
    
    public static void CityListSort(List<ShtCity> arr)
    {
        Collections.sort(arr, new ComparatorCity());
    }
    
    public static void CityNameSort(List<String> arr) {
        Collections.sort(arr, new ComparatorCityName());
    }

    public static void TruckCitySort(List<CityDTO> arr) {
        Collections.sort(arr, new ComparatorCityBrand());
    }
    /**
     * 功能：实现汉语拼音序比较
     * 
     */
    static class ComparatorProvince implements Comparator<ShtProvince>
    {
        @Override
        public int compare(ShtProvince p1, ShtProvince p2)
        {
            return ToPinYinString(p1.getName()).compareTo(ToPinYinString(p2.getName()));
        }
        
        private String ToPinYinString(String str)
        {
            return Pinyin4jUtils.converterToFirstSpell(str);
        }
        
    }
    
    /**
     * 功能：实现汉语拼音序比较
     * 
     */
    static class ComparatorCity implements Comparator<ShtCity>
    {
        @Override
        public int compare(ShtCity p1, ShtCity p2)
        {
            return ToPinYinString(p1.getName()).compareTo(ToPinYinString(p2.getName()));
        }
        
        private String ToPinYinString(String str)
        {
            return Pinyin4jUtils.converterToFirstSpell(str);
        }
        
    }
    
    /**
     * 功能：实现城市名称汉语拼音比较
     * 
     */
    static class ComparatorCityName implements Comparator<String> {
        @Override
        public int compare(String p1, String p2) {
            return ToPinYinString(p1).compareTo(ToPinYinString(p2));
        }

        private String ToPinYinString(String str) {
            return Pinyin4jUtils.converterToFirstSpell(str);
        }

    }

    /**
     * 功能：实现城市名称的汉语拼音比较
     * 
     */
    static class ComparatorCityBrand implements Comparator<CityDTO> {
        @Override
        public int compare(CityDTO p1, CityDTO p2) {
            return ToPinYinString(p1.getName()).compareTo(ToPinYinString(p2.getName()));
        }

        private String ToPinYinString(String str) {
            return Pinyin4jUtils.converterToFirstSpell(str);
        }

    }

    /**
     * 功能：实现汉语拼音序比较
     * 
     */
    static class ComparatorPinYin implements Comparator<String>
    {
        @Override
        public int compare(String o1, String o2)
        {
            return ToPinYinString(o1).compareTo(ToPinYinString(o2));
        }
        
        private String ToPinYinString(String str)
        {
            return Pinyin4jUtils.converterToFirstSpell(str);
        }
        
    }
}
